import Vue from 'vue';
import Es6Promise from 'es6-promise';
import App from './App.vue';
import router from './router';
import store from './store';
import MintUI from 'mint-ui'
import 'mint-ui/lib/style.css'
import './assets/iconfont2/iconfont.css';
import './assets/iconfont2/iconfont.js';
import JSEncrypt from 'jsencrypt'
// import VConsole from 'vconsole/dist/vconsole.min.js' //import vconsole
// let vConsole = new VConsole() // 初始化

// 调试工具
// import VConsole from 'vconsole/dist/vconsole.min.js' //import vconsole
// let vConsole = new VConsole() // 初始化

Vue.use(MintUI)
require('es6-promise').polyfill()
Es6Promise.polyfill()

Vue.config.productionTip = false;

// 消除点击延迟
const FastClick = require('fastclick');
FastClick.attach(document.body);

Vue.prototype.$getRsaCode = function(str) { // 注册方法
    let pubKey = `-----BEGIN PUBLIC KEY-----
    MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC0UyNGFkG3DVA7jtpzof/4Cgmh
    dCr88d8HQEOPWidoX36KY3OXl5+z3WuhAxzcq4lBvJa9XmnSJFavzu6keRpBmK/P
    WwdrNtr7Wq7bLhshooOwvxnGADr1rBhs91JxnWp+0PY4XvT5lNvp7aOnK3Ch8arF
    9O+dR1dD28BbrIp0RQIDAQAB
    -----END PUBLIC KEY-----`; // ES6 模板字符串 引用 rsa 公钥
    let encryptStr = new JSEncrypt();
    encryptStr.setPublicKey(pubKey); // 设置 加密公钥
    let data = encryptStr.encrypt(str.toString()); // 进行加密
    return data;
}


//路由拦截器
router.beforeEach((to, from, next) => {
    if (!to.meta.requireAuth) {
        next();
    } else {
        // store已存在用户信息直接进入页面
        var token = window.localStorage.getItem('user');
        if (token != null) {
            next();
            return
        } else {
            window.localStorage.removeItem('user');
            window.localStorage.removeItem('date');
            // 将跳转的路由path作为参数，登录成功后跳转到该路由
            next({
                path: '/login',
                query: {
                    redirect: to.fullPath
                }
            })
        }

    }
});

new Vue({
    router,
    store,
    render: h => h(App)
}).$mount('#app');